package com.wang.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.auth.service.SysDeptService;
import com.wang.auth.service.SysPostService;
import com.wang.auth.service.SysUserService;
import com.wang.common.result.Result;
import com.wang.model.system.SysDept;
import com.wang.model.system.SysPost;
import com.wang.model.system.SysUser;
import com.wang.model.system.dto.SysUserDto;
import com.wang.model.system.vo.SysUserQueryVo;
import com.wang.security.custom.LoginInfoHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {


    @Autowired
    private SysUserService userService;

    @Autowired
    private SysDeptService deptService;
    @Autowired
    private SysPostService postService;

    //用户条件分页查询
    @ApiOperation("用户条件分页查询")
    @GetMapping("/{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    public Result index(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo sysUserQueryVo){
        Page<SysUserDto> sysUserDtoPage = new Page<>(page,limit);
        return userService.index(sysUserDtoPage,sysUserQueryVo);
    }

    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    public Result get(@PathVariable Long id) {
        SysUser user = userService.getById(id);
        return Result.success(user);
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    public Result save(@RequestBody SysUser user) {
        userService.save(user);
        return Result.success("添加成功！");
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    public Result updateById(@RequestBody SysUser user) {
        userService.updateById(user);
        return Result.success("修改成功！");
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    public Result remove(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success("删除成功！");
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        userService.updateStatus(id, status);
        return Result.success(null);
    }

    //这个请求是冲9090端口传过来的，会产生跨域，所有要加上CrossOrigin注解
    @CrossOrigin
    @GetMapping("/getCurrentUser")
    public Result getCurrentUser(){
        SysUser user = userService.getById(LoginInfoHelper.getUserId().get());
        SysDept dept = deptService.getById(user.getDeptId());
        SysPost post = postService.getById(user.getPostId());
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",user.getName());
        map.put("phone",user.getPhone());
        map.put("deptName",dept.getName());
        map.put("postName",post.getName());
        return Result.success(map);
    }
}
